var BSTYUN = BSTYUN || {};
BSTYUN.check = {
    isPhone: function(){
        var ua = navigator.userAgent.toLowerCase(),
            reg = /iPhone|iPad|Android|ucweb|windows\s+mobile|Windows\s+Phone/i;
            return reg.test(ua);
    },
    //IE6判定
    isIE6: window.VBArray && !window.XMLHttpRequest,
    //判断是否为中英文，数字，下划线，减号
    isNick: function(str) {
        var nickReg = /^[\u4e00-\u9fa5A-Za-z]+$/;
//        var nickReg = /^[\u4e00-\u9fa5A-Za-z0-9-_]+$/;
        return nickReg.test(str);
    },
    //判断邮箱
    isEmail: function(str) {
        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
        return emailReg.test(str);
    },
    //判断手机
    isMobile: function(str) {
        var mobileReg = /^1[345678][0-9]{9}$/;
        return mobileReg.test(str);
    },
    // 判断固话
    isTelephone: function(str) {
        var phoneReg = /^0\d{2,3}-\d{5,9}$/;
        return phoneReg.test(str);
    },
    //判断URL
    isUrl: function(str) {
        var urlReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/;
        return urlReg.test(str);
    },
    //判断数字
    isNum: function(str) {
        var numReg = /^[0-9]\d*$/;
        return numReg.test(str);
    },
    //是否传真
    isFax: function(str){
    	var faxReg = /^0\d{2,3}-\d{5,9}$/;
    	return faxReg.test(str);
    }
};
$(function(){
	/*表单验证*/
	BSTYUN.hbsq.init();
})

BSTYUN.hbsq = {
	flag: {
		belongCompanyName:false,
		realName: false,
		userTel:false,
		fixationTel:false,
		userEmail:false,
		provinceCode: false,
		city: true,
		address: false,
		industry: true,
		scale: true,
		companyIntroduction: true
	},
	init: function(){
		var that = this;
		/*省份下拉框校验*/
		that.change();
		/*表单提交验证*/
		that.formcheck();
		/*失去焦点验证*/
		that.blurFocus();
	},
	formcheck: function(){
		var that = this,
			flag = {},
			$submit = $("#applyBtn"),
			$form = $("#addForm"),
			$inputs = $form.find("input[type='text']");
		$submit.on('click', function(event) {
			var province = $('select[name=provinceCode]'),
				provinceCode = province.val(),
				requestURI = $('#returnUrl').val();
			$.ajax({
				type : "POST",
				url : "/common/checkLogin",
				async : false,
				dataType: 'json',
				success:function(data) {
					if(data) {
						$.each($inputs,function(index, el) {
							var $this = $(this);
							$this.trigger('blur');
						});
						flag = that.flag;
						if(provinceCode == '') {
							that.showError(province,"带*号的不能为空");
						}else {
							that.clearError(province);
						}
						if(flag.realName && flag.belongCompanyName && flag.userTel && flag.fixationTel && flag.userEmail 
							 &&  flag.provinceCode && flag.city && flag.address && flag.industry && flag.scale && flag.companyIntroduction){
							$.ajax({
						        cache: true,
						        type: "POST",
						        url:"/partner/apply/commit?random="+Math.random(),
						        data: $form.serialize(),
						        dataType:'json',
						        async: false,
						        error: function(request) {
						        	BSTYUN.dialog.warn("提交失败，请联系客服");
						        },
						        success: function(data) {
						        	if(data){
						        		location.href="/partner/cooperater/qrtj";
						        	}else{
						        		BSTYUN.dialog.warn("保存失败");
						        	}
						        }
						    });
						}
					}else {
						location.href = "/user/login?returnUrl="+requestURI;
					}
				}
			})
		});
	},
	blurFocus: function(){
		var that = this,
			$form = $("#addForm"),
			$inputs = $form.find("input[type='text']");
		$form.on('blur', "input[type='text']", function(event) {
			var $this = $(this),
				name = $this.attr('name'),
				value = $.trim($this.val());
			if(value == '' || value.length == 0) {
				that.showError($this,"带*号的不能为空");
				return;
			}else {
				switch (name){
					case 'belongCompanyName':
						that.clearError($this);
						break;
					case 'realName':
						if(BSTYUN.check.isNick(value)){
							that.clearError($this);
						}else{
							that.showError($this,'联系人必须是中英文');
						}
						break;
					case 'userTel':
						if(BSTYUN.check.isMobile(value)){
							that.clearError($this);
						}else{
							that.showError($this,'请输入正确的手机号');
						}
						break;
					case 'fixationTel':
						if(BSTYUN.check.isTelephone(value)){
							that.clearError($this);
						}else{
							that.showError($this,'固定电话(格式，区号-号码)');
						}
						break;
					case 'userEmail':
						if(BSTYUN.check.isEmail(value)){
							that.clearError($this);
						}else{
							that.showError($this,'请输入正确的电子邮件地址');
						}
						break;
					case 'address':
						that.clearError($this);
						break;
				};
			}
		}).on('focus', "input[type='text']", function(event) {
			var $this = $(this);
			that.clearError($this);
		});

	},
	/*显示错误信息*/
	showError: function($elem,str){
		var that= this,
			name = $elem.attr('name');
		if(name == 'provinceCode') {
			$elem = $('select[name=areaCode]');
		}
		appendError($elem,str);
		that.flag[name] = false;
		
		function appendError($elem,str){
			var $next = $elem.nextAll('.errormsg');
			if ($next.size() > 0) {
				$next.html(str);
			}else{
				var html = '<span class="errormsg" >'+str+'</span>';
				$elem.after(html);
			}
		}
	},
	/*清空错误信息*/
	clearError : function($elem) {
		var that= this,
			name = $elem.attr('name');
		that.flag[name] = true;
		if(name == 'provinceCode') {
			$elem = $('select[name=areaCode]');
		}
		$elem.nextAll('.errormsg').remove();
	},
	/*省份下拉框改变时的事件*/
	change : function() {
		var $province = $('select[name=provinceCode]'),
			that = this;
		$province.on('change',function(){
			var provinceCode = $province.val();
			if(provinceCode == '') {
				that.showError($province,"带*号的不能为空");
			}else {
				that.clearError($province);
			}
		})
	}
}